import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:pcp/common/config/config.dart';
import 'package:pcp/pages/draw_page.dart';
import 'package:pcp/pages/login_page.dart';
import 'package:pcp/pages/mine_workstation_page.dart';
import 'package:pcp/splash.dart';
import 'package:pcp/pages/mine_page.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

// import 'model/photo_provider.dart';
//
// final provider = PhotoProvider();

void main() => runApp(new App());

class App extends StatelessWidget {
  final String title;

  App({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return OKToast(
      child: MaterialApp(
        theme: new ThemeData(
          primaryIconTheme: const IconThemeData(color: Colors.white),
          brightness: Brightness.light,
          primaryColor: Config.GLOBAL_COLOR,
          accentColor: Config.GLOBAL_COLOR,
        ),
        home: SplashPage(),
      ),
      // child: ChangeNotifierProvider<PhotoProvider>.value(
      //   value: provider,
      //   child: MaterialApp(
      //     theme: new ThemeData(
      //       primaryIconTheme: const IconThemeData(color: Colors.white),
      //       brightness: Brightness.light,
      //       primaryColor: Config.GLOBAL_COLOR,
      //       accentColor: Config.GLOBAL_COLOR,
      //     ),
      //     home: SplashPage(),
      //   ),
      // ),
    );
  }
}

class MainPage extends StatefulWidget {
  MainPage({Key key, this.userName}) : super(key: key);
  final String userName;

  @override
  _MainPageState createState() => new _MainPageState(this.userName);
}

class _MainPageState extends State<MainPage> {
  _MainPageState(String userName) {
    this.username = userName;
  }
  String username;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(username),
        backgroundColor: Colors.redAccent,
        actions: <Widget>[
          IconButton(icon: Icon(Icons.search), onPressed: () {
            Navigator.of(context).push(new MaterialPageRoute(
                builder: (BuildContext context) =>
                new DrawPage()));
          }),
        ],
      ), //头部的标题AppBar
      drawer: new Drawer(
        child: new ListView(
          children: <Widget>[
            new UserAccountsDrawerHeader(
              //Material内置控件
              accountName: new Text(
                username,
                style: TextStyle(
                  color: Colors.black
                ),
              ), //用户名
              accountEmail: new Text(""), //用户邮箱
              currentAccountPicture: new GestureDetector(
                onTap: () async {
                  Navigator.of(context).pop(); //点击后收起侧边栏
                  SharedPreferences prefs = await SharedPreferences.getInstance();
                  if (prefs.getKeys().length == 0)
                    Navigator.of(context).push(new MaterialPageRoute(
                        builder: (BuildContext context) =>
                            new LoginPage()));
                  //进入OtherPage页面，传入参数First Page
                  else
                    Navigator.of(context).push(new MaterialPageRoute(
                        builder: (BuildContext context) =>
                        new MinePage()));
                },
                child: new CircleAvatar(
                  //圆形图标控件
                  backgroundImage: new NetworkImage(
                    Config.BASE_URL + "/user/getProfilePhoto?sex=male"
                  ),
                ),
              ),
              decoration: new BoxDecoration(
                  //用一个BoxDecoration装饰器提供背景图片
//                image: new DecorationImage(
//                    fit: BoxFit.fill,
//                    image: new NetworkImage('https://raw.githubusercontent.com/flutter/website/master/_includes/code/layout/lakes/images/lake.jpg')
//                ),
                  ),
            ),
            new ListTile(
                //第一个功能项
                title: new Text('工作站'),
                trailing: new Icon(Icons.perm_media),
                onTap: () async {
                  SharedPreferences prefs = await SharedPreferences.getInstance();
                  if (prefs.getKeys().length == 0)
                    showToast("请先去登录！");
                  else {
                    Navigator.of(context).pop(); //点击后收起侧边栏
                    Navigator.of(context).push(new MaterialPageRoute(
                        builder: (BuildContext context) =>
                        new MineWorkStationPage()));
                  }
                }),
            new ListTile(
                //第三个功能项
                title: new Text('我的钱包'),
                trailing: new Icon(Icons.account_balance_wallet),
                onTap: () async {
                  SharedPreferences prefs = await SharedPreferences.getInstance();
                  if (prefs.getKeys().length == 0)
                    showToast("请先去登录！");
                  else
                    Navigator.of(context).pop();
                }),
            new Divider(), //分割线控件
            new ListTile(
              //退出按钮
              title: new Text('退出登录'),
              trailing: new Icon(Icons.logout),
                onTap: () async {
                  SharedPreferences prefs = await SharedPreferences.getInstance();
                  prefs.clear();
                  Navigator.of(context).pop();
                  // Navigator.of(context).push(new MaterialPageRoute(
                  //     builder: (BuildContext context) => new LoginPage()));
                  setState(() {
                    username = "请登录";
                  });
                }),
          ],
        ),
      ), //侧边栏按钮Drawer
      body: new Center(
        //中央内容部分body
        child: new Text(
          'HomePage',
          style: new TextStyle(fontSize: 35.0),
        ),
      ),
    );
  }
}
